import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import config from '@/config/config'
import request from '@/static/request'
const store = new Vuex.Store({
    state: {
        forcedLogin: config.isNeedLogin || false, //是否强制登录
        hasLogin: uni.getStorageSync('access_token') ? true : false, //是否已登录
		loginStatus:false,
		userAddrInfo: JSON.parse(uni.getStorageSync("user_addr_info") || "{}"),
		userInfo: uni.getStorageSync('userInfo') || {
			city: "",
			country: "",
			land_time: "",
			province: "",
			sex: 0,
			user_code: "",
			user_name: "",
			user_guid: "",
			user_phone: "",
			headimgurl: ""
		},
		msgChatList:[],
		newMsgNum:0
    },
    mutations: {
		setUserInfo(state,userInfo){
			if(userInfo){
				state.userInfo = userInfo;
				uni.setStorageSync("userInfo",userInfo);
				uni.setStorageSync("headImgurl",userInfo.headimgurl);
			}
		},
        login(state,data,resolve) {
			if(data){
				uni.setStorageSync('access_token', data.access_token || "" );
				uni.setStorageSync('refresh_token', data.refresh_token || "");
				if(data.userinfo) this.commit("setUserInfo", data.userinfo );
			}
			state.hasLogin = true;
			if(!resolve) { state.loginStatus = !state.loginStatus };
        },
        logout(state) {
			uni.setStorageSync("access_token","");
			uni.setStorageSync("refresh_token","");
			state.hasLogin = false;
			state.loginStatus = !state.loginStatus;
        },
		//微信小程序登录
		wxappLogin(state,resolve,reject){
			var that = this;
			uni.showLoading({ title: '', mask:true});
			wx.login({
				success (res) {
					if (res.code) {
						var js_code = res.code;
						wx.getUserInfo({// 获取微信用户信息
							lang:"zh_CN",
							success: function(res) {
								uni.hideLoading();
								let userInfo = res.userInfo;
								request('/app/soft/wx/wxAppLogin',{
									userInfo:JSON.stringify(userInfo),
									code:js_code,
								}).then(function(res){
									if(res.data){
										uni.showToast({ title:"登录成功!"});
										uni.setStorageSync('access_token', res.data.access_token || "" );
										uni.setStorageSync('refresh_token', res.data.refresh_token || "");
										that.commit("setUserInfo", res.data.userinfo );
										state.hasLogin = true;
										if(resolve){ resolve(); }
									}
								});
							},
							fail:res=>{
								uni.hideLoading();
								if(reject){ reject(); }
							}
						})
					}
				}
			});
		},
		// 设置客服信息列表
		setCsdMsgChatList(state,msgChatList){
			if(msgChatList){
				let newMsgNum = 0;
				msgChatList.forEach(function(h){
					if(h.tis_num > 0){
						newMsgNum += h.tis_num;
					}
				});
				state.newMsgNum=newMsgNum;
				state.msgChatList = msgChatList;
			}
		},
		setUserAddrInfo(state,info){
			if(info){
				state.userAddrInfo = info;
				uni.setStorageSync("user_addr_info",JSON.stringify(info));
			}
		},
    },
	actions:{
		// 自动选择模式登录
		autoModeLogin({commit},mode){
			return  new Promise((resolve, reject) => {  
				if(config.webMode == 'wx-web' && config.wxWebLoginUrl() ){
					window.location.replace( config.wxWebLoginUrl() );
				}else if(config.webMode == 'wx-app'){
					if(mode==1) return false;
					commit("wxappLogin",resolve,reject);
				}else{
					uni.navigateTo({ url: '/pages/login/login'});
				}
			});
		}
	}
})

export default store
